<a href="<?php echo $this->webroot; ?>">Inicio</a> |
<a href="<?php echo $this->webroot; ?>users/products"> Subir mas</a><br/>
<?php
include("SimpleImage.ctp");
$status = "";
if ($_POST["action"] == "upload") {
	// obtenemos los datos del archivo
	$tamano = $_FILES["archivo"]['size'];
	$type = $_FILES["archivo"]['type'];
	$archivo = $_FILES["archivo"]['name'];
	$archivo = end(explode(".",low($archivo)));
	$archivo = ".".$archivo;
	$prefijo = substr(md5(uniqid(rand())),0,15);
	$prefijo = $prefijo.$archivo;
	echo "Tamaño: ".$tamano." KB<br/>";
	echo "Tipo: ".$type."<br/>";
	
	if ($archivo != "") {
		// guardamos el archivo a la carpeta files
		$destino =  "img/upload/".$prefijo;
		
		if ($_FILES['archivo']['error'] == 0) {
			$newImage = new SimpleImage();
			$newImage->load($_FILES["archivo"]['tmp_name']);
			if ($newImage->getWidth()>900){
				$newImage->resizeToWidth(900);
			}
			if ($newImage->getHeight()>700){
				$newImage->resizeToHeight(700);
			}
			$newImage->save($destino);
			if ($newImage->getWidth()>300){
				$newImage->resizeToWidth(300);
			}
			if ($newImage->getHeight()>150){
				$newImage->resizeToHeight(150);
			}
			$newImage->save("img/upload/tb".$prefijo);
			chmod($destino,0777);

			//QUERY PARA INSERTAR LA FOTO EN PRODUCTOS Y FOTO
			
			$idSubcategory = ClassRegistry::init('Subcategory')->find('first', Array('conditions' => Array('Subcategory.code' => $_POST['my_combobox'])));
			$idSubcategory = $idSubcategory['Subcategory']['id'];

			$this->data['Product']['name'] = $_POST['name'];
			$this->data['Product']['subcategory_id'] = $idSubcategory;
			App::import('Model','Product');
			$product = new Product;
			
			//Si se puede insertar el producto
			if ($product->save($this->data)){ 
				
				$idProduct = ClassRegistry::init('Product')->find('first', Array('conditions' => Array('Product.name' => $_POST['name'])));
				$idProduct = $idProduct['Product']['id'];
					
				$this->data['Image']['url_full'] = $prefijo;
				$this->data['Image']['url_thumbnail'] = "tb".$prefijo;
				$this->data['Image']['product_id'] = $idProduct;
				App::import('Model','Image');
				$image= new Image;
				$image->save($this->data);
				
				
				
				echo "<br/>";
				
				// GESTION DE REDIMENSIONAMIENTO /////////////////////////////////
				/*list($width, $height) =  ($destino);
				echo "<h1>Es de tipo: ";print_r($type);echo "</h1>";
				if ($width > $height){
					
					//Create a thumbnail
					$diff = $width-202;
					$porcent_reducc = (($diff/$width)*100);
					$new_height = $height - (($porcent_reducc*$height)/100);
					$new_width = $width - $diff;
						
					//LOAD
					$thumb = imagecreatetruecolor($new_width, $new_height);
					if ($type == "jpg" || $type == "jpeg" || $type == "JPG"){
						$source = imagecreatefromjpeg($destino);
					}else if ($type == "png" || $type == "image/png" || $type == "image/PNG"){
						$source = imagecreatefrompng($destino);
					}else if ($type == "gif" || $type == "image/gif" || $type == "image/GIF"){
						$source = imagecreatefromgif($destino);
					}else if ($type == "image/jpg" || $type == "image/jpeg" || $type == "image/JPG"){
						$source = imagecreatefromjpeg($destino);
					}else {
						echo "Error de tipo, contacte con el administrador. <br/>";
					}
						
					print_r("asdf: ".$thumb."<br/>".$source."<br/>");
					//RESIZE
					imagecopyresized($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
					//Grab de image
					$destinoTb =  "img/upload/"."tb".$prefijo;
					imagejpeg($thumb, $destinoTb);
					$image_buffer = ob_get_contents();
					echo "$destinoTb redimensianado a $new_width $new_height <br /> \n";
					
					if ($width > 900){
						$diff = $width-900;
						$porcent_reducc = (($diff/$width)*100);
						$new_height = $height - (($porcent_reducc*$height)/100);
						$new_width = $width - $diff;
						
						//LOAD
						$thumb = imagecreatetruecolor($new_width, $new_height);
						if ($type == "jpg" || $type == "jpeg" || $type == "JPG"){
							$source = imagecreatefromjpeg($destino);
						}else if ($type == "png" || $type == "image/png" || $type == "image/PNG"){
							$source = imagecreatefrompng($destino);
						}else if ($type == "gif" || $type == "image/gif" || $type == "image/GIF"){
							$source = imagecreatefromgif($destino);
						}else if ($type == "image/jpg" || $type == "image/jpeg" || $type == "image/JPG"){
							$source = imagecreatefromjpeg($destino);
						}else {
							echo "Error de tipo, contacte con el administrador. <br/>";
						}
						//RESIZE
						imagecopyresized($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
						//Grab de image
						imagejpeg($thumb, $destino);
						$image_buffer = ob_get_contents();
						imagedestroy($thumb);
						imagedestroy($source);
						echo "$destino redimensianado a $new_width $new_height <br /> \n";

					}

					
				}else {
					//THUMBNAIL CREATION
					$diff = $height-160;
					$porcent_reducc = (($diff/$height)*100);
					$new_height = $height - $diff;
					$new_width = $width - (($porcent_reducc*$width)/100);
						
					//LOAD
					$thumb = imagecreatetruecolor($new_width, $new_height);
					if ($type == "jpg" || $type == "jpeg" || $type == "JPG"){
						$source = imagecreatefromjpeg($destino);
					}else if ($type == "png" || $type == "image/png" || $type == "image/PNG"){
						$source = imagecreatefrompng($destino);
					}else if ($type == "gif" || $type == "image/gif" || $type == "image/GIF"){
						$source = imagecreatefromgif($destino);
					}else if ($type == "image/jpg" || $type == "image/jpeg" || $type == "image/JPG"){
						$source = imagecreatefromjpeg($destino);
					}else {
						echo "Error de tipo, contacte con el administrador. <br/>";
					}
					//RESIZE
					imagecopyresized($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
					//Grab de image
					$destinoTb =  "img/upload/"."tb".$prefijo;
					imagejpeg($thumb, $destinoTb);
					$image_buffer = ob_get_contents();
					echo "$destinoTb redimensianado a $new_width $new_height <br /> \n";
					
					if ($height > 700){
						$diff = $height-700;
						$porcent_reducc = (($diff/$height)*100);
						$new_height = $height - $diff;
						$new_width = $width - (($porcent_reducc*$width)/100);
						
						//LOAD
						$thumb = imagecreatetruecolor($new_width, $new_height);
						if ($type == "jpg" || $type == "jpeg" || $type == "JPG"){
							$source = imagecreatefromjpeg($destino);
						}else if ($type == "png" || $type == "image/png" || $type == "image/PNG"){
							$source = imagecreatefrompng($destino);
						}else if ($type == "gif" || $type == "image/gif" || $type == "image/GIF"){
							$source = imagecreatefromgif($destino);
						}else if ($type == "image/jpg" || $type == "image/jpeg" || $type == "image/JPG"){
							$source = imagecreatefromjpeg($destino);
						}else {
							echo "Error de tipo, contacte con el administrador. <br/>";
						}
						//RESIZE
						imagecopyresized($thumb, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
						//Grab de image
						imagejpeg($thumb, $destino);
						$image_buffer = ob_get_contents();
						imagedestroy($thumb);
						imagedestroy($source);
						echo "$destino redimensianado a $new_width $new_height <br /> \n";
					}
					
					
				}*/
				
				// /////////////////////////////////////////////////// /////////////////////////////////
				
				
				echo "<br/>";
				
				$status = "Archivo subido: <b>".$archivo."</b> Prefijo:".$destino;
				print_r($status);
				
				
			}else {
				echo "<b>Error, no se pueden insertar 2 productos con el mismo nombre</b>";
				unlink($destino);
			}
			
		} else {
			$status = "PHPERROR:{$_FILES['archivo']['error']}:Error copiando el archivo";
			print_r($status);
		}
	} else {
		$status = "Error al subir archivo";
		print_r($status);
	}
}else {
	echo "No hay ninguna foto seleccionada";
}
?>